package com.linln.modules.shopsService.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.linln.modules.shopsService.domain.Shops;
import com.linln.modules.shopsService.repository.ShopReviewedRepository;
import com.linln.modules.shopsService.repository.ShopsRepository;
import com.linln.modules.shopsService.service.ShopReviewedService;
import com.linln.modules.shopsService.service.ShopsService;
import io.lettuce.core.Limit;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.LimitExceededException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.modules.shopsService.service.impl
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.modules.shopsService.service.impl
 * @Description: TODO
 * @date Date : 2020年12月09日 15:18
 */
@Service
public class ShopReviewedServiceImpl implements ShopReviewedService {

    @Autowired
    private ShopReviewedRepository shopReviewedRepository;

    @Autowired
    private ShopsRepository shopsRepository;

    @Autowired
    private ShopsService shopsService;

    @Override
    /**
     * 获取店铺通过店铺状态 或店铺名称 和 店铺负责人名字
     */
    public Map<String, Object> getShopsByStatus(Integer page, Integer limit, Integer status, String searchName) {
        Page<Shops> shopsByStatus = shopReviewedRepository.getShopsByStatus(status, searchName, new PageRequest(page - 1, limit));
//        Page<Map<String, String>> shops = shopReviewedRepository.getShopsByStatus(status, searchName, new PageRequest(page - 1, limit));
        shopsByStatus.getContent().forEach(item -> {
            item.setShopTypeName(shopReviewedRepository.selectShopType(item.getShopId()));
        });
        Map<String, Object> map = new HashMap<>();
//        map.put("code", 0);
        map.put("data", shopsByStatus.getContent());
        map.put("count", shopsByStatus.getTotalElements());
        return map;
    }

    @Override
    @Transactional
    public boolean reviewed(String shopId, Integer reviewedStatus, String reviewedOpinionText) {
        try {
            shopReviewedRepository.updateReviewedStatus(shopId, reviewedStatus, reviewedOpinionText);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public String selectShopType(String shopId) {
        return shopReviewedRepository.selectShopType(shopId);
    }

    @Override
    public List<Shops> getUpdateShopAudit(String searchName) throws JSONException, UnsupportedEncodingException {
        List<Shops> shopsList = shopsRepository.findAll();
        List<Shops> newShopList = new ArrayList<>();

        for(Shops shops : shopsList){
            JSONObject old = new JSONObject(shops.getPropsJson());
            if(old.containsKey("newShop")){
                if(old.get("newShop")!=null){
                    String bytesJson = String.valueOf(old.get("newShop"));
                    List<Byte> list = JSONObject.parseArray(bytesJson, Byte.class);
                    Byte[] bytes = list.toArray(new Byte[list.size()]);
                    byte[] bytes1 = new byte[list.size()];
                    for(int i = 0;i<bytes.length;++i){
                        bytes1[i] = bytes[i];
                    }
                    String json = new String(bytes1,"UTF-8");
                    JSONObject newshop = JSONObject.parseObject(json);
                    Shops newShops = new Shops();
                    newShops.setShopId(shops.getShopId());
                    newShops.setShopName(newshop.get("shopName").toString());
                    newShops.setShopTypeName(newshop.get("shopTypeName").toString());
                    newShops.setContactsName(newshop.get("contactsName").toString());
                    newShops.setPhone(newshop.get("phone").toString());
                    newShops.setSparePhone(newshop.get("sparePhone").toString());
                    newShops.setAddress(newshop.get("address").toString());
                    newShops.setShopPicture(newshop.get("shopPicture").toString());
                    newShops.setCreateDate(newshop.get("createDate").toString());
                    JSONObject props = new JSONObject();
                    props.put("technicianconstructionMapping",newshop.get("technicianconstructionMapping").toString());
                    props.put("constructionworkshopMapping",newshop.get("constructionworkshopMapping").toString());
                    props.put("businesslicenseMapping",newshop.get("businesslicenseMapping").toString());
                    newShops.setProps(props.toString());
                    if(StringUtils.isNotBlank(searchName)){
                        if(searchName.contains(newShops.getShopName())){
                            newShopList.add(newShops);
                            continue;
                        }
                    }
                    newShopList.add(newShops);
                }
            }
        }
        return newShopList;
    }

    @Override
    public void reviewedUpdate(String shopId, boolean status) throws UnsupportedEncodingException {
        Shops newShops = shopsService.getById(shopId);
        JSONObject old = JSONObject.parseObject(newShops.getProps());
        if(status){
            if(old.containsKey("newShop")){
                if(old.get("newShop")!=null){
                    String bytesJson = String.valueOf(old.get("newShop"));
                    List<Byte> list = JSONObject.parseArray(bytesJson, Byte.class);
                    Byte[] bytes = list.toArray(new Byte[list.size()]);
                    byte[] bytes1 = new byte[list.size()];
                    for(int i = 0;i<bytes.length;++i){
                        bytes1[i] = bytes[i];
                    }
                    String json = new String(bytes1,"UTF-8");
                    System.out.println(json);
                    JSONObject newshop = JSONObject.parseObject(json);
                    newShops.setShopName(newshop.get("shopName").toString());
                    newShops.setShopType(newshop.get("shopType").toString());
                    newShops.setAreaId(newshop.get("areaId").toString());
                    newShops.setLongitude(newshop.get("longitude").toString());
                    newShops.setLatitude(newshop.get("latitude").toString());
                    newShops.setContactsName(newshop.get("contactsName").toString());
                    newShops.setPhone(newshop.get("phone").toString());
                    newShops.setSparePhone(newshop.get("sparePhone").toString());
                    newShops.setAddress(newshop.get("address").toString());
                    newShops.setShopPicture(newshop.get("shopPicture").toString());
                    old.put("shopTypeName",newshop.get("shopTypeName").toString());
                    old.put("technicianconstructionMapping",newshop.get("technicianconstructionMapping").toString());
                    old.put("constructionworkshopMapping",newshop.get("constructionworkshopMapping").toString());
                    old.put("businesslicenseMapping",newshop.get("businesslicenseMapping").toString());
                    old.remove("newShop");
                    newShops.setProps(old.toString());
                    shopsRepository.save(newShops);
                }
            }
        }else {
            old.remove("newShop");
            newShops.setProps(old.toString());
            shopsRepository.save(newShops);
        }
    }
}
